#!/usr/bin/env perl 

# 将 INDEX_RAW 数据导出
use Data::Dumper;
use File::Basename;

my ($raw_index) = @ARGV;
$USAGE = qq{
USAGE: 
  ./trip_data.pl ./DATA/13/1094941/INDEX_RAW
};

open LOG_FILE, ">>logs/strip_data.log";

if (!defined($raw_index)) {
	die  $USAGE;
}

if ( -z $raw_index ) {
	print LOG_FILE "[$raw_index] ERROR\n";
	die;
}

$dirname = dirname $raw_index;
$result_file = $dirname. "/INDEX_DONE";

$data = `cat $raw_index`;

open TARGET_FILE, ">$result_file";

my @sections; 
# 先分解具体的vol区块

while ($data =~ /(<div class='chat'><strong>.*?<\/table><\/div>)/gi) {
	my $section = $1;
	push @sections, $section;
}

foreach my $section (@sections) {
	$section =~ /<div class='chat'><strong>(.*?)<\/strong>/; 
	$vol_name = $1;
	$vol_name =~s/^\s*(.*)\s*$/$1/;
	chomp ($vol_name);
	next if (!defined $vol_name);
	print TARGET_FILE $vol_name . "|";
	while ( $section =~ /<td [^>]* class=['"]chaptertd['"]><a href='[^>]*>(.*?)<\/a>/gi ) {
		my $chap = $1;
		$chap =~s/^\s*(.*)\s*$/$1/;
		chomp($chap);
		print TARGET_FILE $chap . "^";
	}
	print TARGET_FILE "\n";
}

if ( -f $result_file ) {
	print LOG_FILE "[$raw_index] DONE\n";
}

close TARGET_FILE;
close LOG_FILE;
